package com.code.refactoring.重构demo.状态机扭转.stateProcessor;

/**
 * @author wangxi03 created on 2021/4/29 10:42 上午
 * @version v1.0
 */

import com.code.refactoring.重构demo.状态机扭转.OrderProcessor;
import com.code.refactoring.重构demo.状态机扭转.check.Checkable;
import com.code.refactoring.重构demo.状态机扭转.check.Checker;
import com.code.refactoring.重构demo.状态机扭转.check.CreateParamChecker;
import com.code.refactoring.重构demo.状态机扭转.check.UnFinishChecker;
import com.code.refactoring.重构demo.状态机扭转.check.UserChecker;
import com.code.refactoring.重构demo.状态机扭转.entity.CreateOrderContext;
import com.code.refactoring.重构demo.状态机扭转.entity.OrderInfo;
import com.code.refactoring.重构demo.状态机扭转.entity.ServiceResult;
import com.code.refactoring.重构demo.状态机扭转.entity.StateContext;
import com.code.refactoring.重构demo.状态机扭转.enums.OrderStateEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 状态A对应的状态处理器
 */
@Component
@Slf4j
@OrderProcessor(state = "INIT", bizCode = {"CHEAP","POPULAR"}, sceneId = "H5")
public class StateCreateTemplateProcessor extends AbstractStateTemplateProcessor<String, CreateOrderContext> {
    @Resource
    private CreateParamChecker<String, CreateOrderContext> createParamChecker;
    @Resource
    private UserChecker<String, CreateOrderContext> userChecker;
    @Resource
    private UnFinishChecker<String, CreateOrderContext> unFinishChecker;

    @Override
    public Checkable getCheckable(StateContext<CreateOrderContext> context) {
        return new Checkable() {
            @Override
            public List<Checker> getParamChecker() {
                return Arrays.asList(createParamChecker);
            }
            @Override
            public List<Checker> getSyncChecker() {
                return Collections.EMPTY_LIST;
            }
            @Override
            public List<Checker> getAsyncChecker() {
                return Arrays.asList(userChecker, unFinishChecker);
            }
        };
    }

    @Override
    public String getNextState(StateContext<CreateOrderContext> context) {
        // if (context.getOrderStateEvent().getEventType().equals("xxx")) {
        //     return OrderStateEnum.INIT;
        //  }
        return OrderStateEnum.NEW.name();
    }
    @Override
    public ServiceResult<String> save(String nextState, StateContext<CreateOrderContext> context) throws Exception {
        OrderInfo orderInfo = context.getContext().getOrderInfo();
        // 更新状态
        orderInfo.setOrderState(nextState);
        // 持久化
//        this.updateOrderInfo(orderInfo);
        log.info("save BUSINESS order success, userId:{}, orderId:{}", orderInfo.getUserId(), orderInfo.getOrderId());
        return new ServiceResult<>(true, orderInfo.getOrderId());
    }

    @Override
    public ServiceResult<String> action(String nextState, StateContext<CreateOrderContext> context) throws Exception {
        System.out.println(StateCreateTemplateProcessor.class.getSimpleName());
        return null;
    }

    @Override
    public void after(StateContext<CreateOrderContext> context) {

    }
}
